/*
* License:  This  program  is  free  software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the  Free Software Foundation; either version 3 of the License, or (at your
* option)  any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*/

package pcgen.CharacterViewer.resources;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

import pcgen.android.Logger;

public class GameResource extends GameResourceParcelable
{
	public GameResource()
	{
	}
	
	public GameResource(Parcel in)
	{
		super(in);
	}
	
	public GameResource(String name, String location, ResourceTypes type)
	{
		_name = name;
		_location = location;
		_type = type;
	}
	
	public GameFolder getFolder()
	{
		return _folder;
	}
	
	public String getLocation()
	{
		return _location;
	}
	
	public String getName()
	{
		return _name;
	}
	
	public ResourceTypes getType()
	{
		return _type;
	}
	
	public void init(GameFolder folder)
	{
		_folder = folder;
	}
	
	public void initFromLocation()
		throws Throwable
	{
	}
	
	public JSONObject load(JSONObject data)
		throws JSONException
	{
		setName(data.getString("name"));
		setLocation(data.getString("location"));
		setType(ResourceTypes.valueOf(data.getString("type")));
	    
	    return data;
	}
	
	public JSONObject save() 
		throws JSONException
	{
	    JSONObject data = new JSONObject();
	    
	    data.put("name", getName());
	    data.put("location", getLocation());
	    data.put("type", getType().toString());
	    
	    return data;
	}
	
	public void setLocation(String location)
	{
		_location = location;
	}
	
	public void setName(String name)
	{
		_name = name;
	}
	
	public void setType(ResourceTypes type)
	{
		_type = type;
	}
	
	public static final Parcelable.Creator<GameResource> CREATOR = new Parcelable.Creator<GameResource>() 
	{
		public GameResource createFromParcel(Parcel in) 
		{
		    return new GameResource(in);
		}
		
		public GameResource[] newArray(int size) 
		{
		    return new GameResource[size];
		}
	};

	@Override
	protected void readFromParcelTransform(Parcel in) 
		throws Throwable
	{
		try
    	{
			setName(in.readString());
			setLocation(in.readString());
			setType(ResourceTypes.valueOf(in.readString()));
		}
    	catch (Throwable tr)
    	{
    		Logger.e(TAG, "readFromParcelTransform", tr);
    		throw tr;
    	}
	}

	@Override
	protected void writeToParcelTransform(Parcel dest, int flags)
			throws Throwable
	{
		try
    	{
			dest.writeString(getName());
			dest.writeString(getLocation());
			dest.writeString(getType().toString());
		}
    	catch (Throwable tr)
    	{
    		Logger.e(TAG, "writeToParcelTransform", tr);
    		throw tr;
    	}
	}

	private String _name;
	private String _location;
	private ResourceTypes _type;
	
	private GameFolder _folder;

	public enum ResourceTypes
	{
		Character,
		Pdf,
		Web
	}

	private static final String TAG = GameResource.class.getSimpleName();
}